package com.ems.sys.service.impl;
import com.ems.sys.dao.DepartmentMapper;
import com.ems.sys.pojo.Department;
import com.ems.sys.service.DepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
/****
 * @Author:传智播客
 * @Description:Department业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;


    /**
     * Department条件+分页查询
     * @param department 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Department> findPage(Department department, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(department);
        //执行搜索
        return new PageInfo<Department>(departmentMapper.selectByExample(example));
    }

    /**
     * Department分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Department> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Department>(departmentMapper.selectAll());
    }

    /**
     * Department条件查询
     * @param department
     * @return
     */
    @Override
    public List<Department> findList(Department department){
        //构建查询条件
        Example example = createExample(department);
        //根据构建的条件查询数据
        return departmentMapper.selectByExample(example);
    }


    /**
     * Department构建查询对象
     * @param department
     * @return
     */
    public Example createExample(Department department){
        Example example=new Example(Department.class);
        Example.Criteria criteria = example.createCriteria();
        if(department!=null){
            // 部门编号
            if(!StringUtils.isEmpty(department.getdNum())){
                    criteria.andEqualTo("dNum",department.getdNum());
            }
            // 閮ㄩ棬缂栧彿
            if(!StringUtils.isEmpty(department.getdName())){
                    criteria.andEqualTo("dName",department.getdName());
            }
            // 閮ㄩ棬缁忕悊缂栧彿
            if(!StringUtils.isEmpty(department.getcName())){
                    criteria.andEqualTo("cName",department.getcName());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        departmentMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Department
     * @param department
     */
    @Override
    public void update(Department department){
        departmentMapper.updateByPrimaryKey(department);
    }

    /**
     * 增加Department
     * @param department
     */
    @Override
    public void add(Department department){
        departmentMapper.insert(department);
    }

    /**
     * 根据ID查询Department
     * @param id
     * @return
     */
    @Override
    public Department findById(String id){
        return  departmentMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Department全部数据
     * @return
     */
    @Override
    public List<Department> findAll() {
        return departmentMapper.selectAll();
    }
}
